<?php
/**
 * Model class link to entered_courses in database
 *
 * @author Dat Nguyen <datnt@evolable.asia>
 */
class EnteredCourse extends AppModel {
	var $name = 'EnteredCourse';
	var $useTable = 'entered_courses';

	/**
	 * Check if user have entered course
	 *
	 * @param $detailId
	 * @param $userId
	 * @return boolean, true if joined, else false
	 */
	function isEnteredCourse($detailId, $userId) {
		$options = array(
			'conditions' => array(
				'del_flg' => 0,
				'detail_id' => $detailId,
				'user_id' => $userId,
			)
		);
		if ($this->find('count', $options)) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * Update del_flg for entered_courses table when user leave a course
	 *
	 * @param $detailId
	 * @param $userId
	 */
	function leaveCourse($detailId, $userId) {
		$conditions = array(
			'user_id' => $userId,
			'detail_id' => $detailId,
		);
		$fields = array(
			'del_flg' => 1,
		);
		$this->updateAll($fields, $conditions);
	}
	
	/**
	 * after edit schedule, insert user list to notify
	 * @param array $courseData
	 * @param string $template
	 */
	function insertUsersOfCourseDetailToSendEmail($courseData, $template) {
		App::import('Core', array('File')); 
		App::import('Model', array('Profile', 'QueueEmail'));
		$profileModel = new Profile();
		$file = new File(VIEWS.'elements/email/html/'. $template);
		$file->open();
		$content = $file->read();
		$file->close();
		$params['fields'] = array('User.email, User.id');
		$params['recursive'] = -1;
		$params['joins'] = array(
			array(
				'table' => 'users',
				'alias' => 'User',
				'type' => 'inner',
				'conditions' => array(
					$this->alias.'.user_id = User.id',
					'User.del_flg' => 0,
					$this->alias.'.detail_id' => $courseData['detail_id'],
					$this->alias.'.del_flg' => 0
				)
			)
		);
		$params['conditions'] = array(
			$this->alias.'.detail_id' => $courseData['detail_id'],
			$this->alias.'.del_flg' => 0
		);
		$result = $this->find('all', $params);
		$openDateArr = date_parse($courseData['openning_date']);
		$openDate = $openDateArr['year'].'/'.$openDateArr['month'].'/'.$openDateArr['day'].' '.$openDateArr['hour'].'時'.$openDateArr['minute'].'分';
		$today = strtotime(date('Y-m-d H:i:s'));
		if(is_array($result)){
			foreach ($result as $key => $user) {
				$queueEmailModel = new QueueEmail();
				$profile = $profileModel->find('first', array(
					'fields' => array($profileModel->alias.'.first_name', $profileModel->alias.'.last_name'),
					'conditions' => array(
						$profileModel->alias.'.user_id' => $user['User']['id']
					)
				));
				$mailBody = str_replace(array('{userName}','{website}', '{course_name}', '{yyyy/mm/dd hh時mm分}', '{course_url}'),
						array($profile[$profileModel->alias]['last_name'].' '.$profile[$profileModel->alias]['first_name'],FULL_BASE_URL, $courseData['course_name'], $openDate, $courseData['course_url']), 
						$content);
				$data = array(
					$queueEmailModel->alias => array(
						'subject' => Messages::email_subject_edit_schedule,
						'email_to' => json_encode($user['User']['email']),
						'content' => $mailBody,
						'status' => QueueEmail::EMAIL_STATUS_NOT_YET_SEND,
						'created' => $today,
						'email_type' => QueueEmail::EMAIL_TYPE_COURSE,
					)
				);
				$queueEmailModel->save($data);
			}
		}
	}
}